package vip.lsjscl.flowboot.util.converter;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import vip.lsjscl.flowboot.workflow.model.NodeData;

import java.util.ArrayList;
import java.util.List;

/**
 * 操作按钮转换器
 * 用于将操作按钮列表与JSON字符串之间进行转换
 *
 * @author zhangxingju
 * @date 2025/02/13
 */
@Converter
public class OperationsConverter implements AttributeConverter<List<NodeData.Operation>, String> {

    @Override
    public String convertToDatabaseColumn(List<NodeData.Operation> operations) {
        return operations != null ? JSONObject.toJSONString(operations) : null;
    }

    @Override
    public List<NodeData.Operation> convertToEntityAttribute(String json) {
        if (StringUtils.isBlank(json)) {
            return new ArrayList<>();
        }
        return JSONArray.parseArray(json, NodeData.Operation.class);
    }
} 